<?php

class salesreceiptsreport
{
	public function salesreceiptsreport($params)
	{
		foreach($params as $key => $value) {
			$this->{$key}=$value;
		}
	}
	public function getshift($date) {
		if(!$date)$date=date("Y-m-d H:i:s");
		list($d, $t) = explode(" ", $date);
		list($h, $m, $s) = explode(":", $t);
		$sql = "select shift_id from shifts where $h between shift_start and shift_end";
		$res = mysql_query($sql) or die(mysql_error() .$sql);
		list($shift)=mysql_fetch_row($res);
		
		if($h==16||$h==15)
		{
			$this->shiftnum=3;
			return $shift = "3rd";
		}
		elseif($h==8||$h==7)
		{
			$this->shiftnum=2;
			return $shift = "2nd";
		}
		$this->shiftnum=1;
	return  "1st";
	}
	
	public function getLatestShifts($shiftid)
	{
		$sql = "SELECT `shift-transaction_id`,datetime,user_id  FROM `shift-transactions` where shift = 'start' order by datetime desc ";
		$res = mysql_query($sql);
		$ret = "<select name='rblshifts' id='rblshifts' onchange='myform.submit();'>";
		$ret .= "<option value=''>&nbsp;</option>";
		while(list($shift_transaction_id,$datetime,$userid)=mysql_fetch_row($res))
		{
			if($shiftid == $shift_transaction_id)
			{
				$select = "selected";
			}else
			{
				$select = " ";
			}
			$__sql = "SELECT user_id  FROM `shift-transactions` where shift = 'end' 
			and `datetime` > '$datetime'
			order by datetime asc 
			limit 0,1";
			$__res = mysql_query($__sql);
			list($userid)=mysql_fetch_row($__res);
			$_sql = "select fullname from users where user_id = '$userid'";
			$_res = mysql_query($_sql);
			list($username)=mysql_fetch_row($_res);
			$ret .= "<option value='$shift_transaction_id' $select>$datetime - $username - ".$this->getshift($datetime)."</option>";
		}
		$ret .= "</select>";
		return $ret;
	}
	
	public function getCashOnHand()
	{
		$sql = "select amount,datetime from cash_on_hand where datetime >='$this->start' and datetime <= '$this->end' ";
		$res = mysql_query($sql);
		$row = mysql_fetch_row($res);
		$this->cashOnHand=$row[0];
		return '<h3>Declared Cash On Hand ['. $row[1] . ']  -  Php ' . number_format($this->cashOnHand,2) .'</h3>';
	}
	
	public function getSafeKeep()
	{

		$sql = "select a.safekeep_date, a.amount, b.fullname as 'cashier', c.fullname as 'oic' 
				from safekeep a, users b, users c
				where a.cashier_id=b.user_id
				and a.oic_id=c.user_id
				and a.safekeep_date >='$this->start' and a.safekeep_date <='$this->end'
				order by a.safekeep_date
			";
		$res = mysql_query($sql) or die($sql);
		if(mysql_num_rows($res)) {
			$ret = '<h3>Safekeeping Records</h3>';
			$ret .= '<table class=report>';
			$ret.='<tr><th>Date</th><th>Amount</th><th>Cashier</th><th>OIC</th></tr>';
			while( list($date,$amount,$cashier,$oic)=mysql_fetch_row($res) ) {
				$ret.='<tr>';
				$ret.="<td>$date</td>";
				$ret.="<td>$amount</td>";
				$ret.="<td>$cashier</td>";
				$ret.="<td>$oic</td>";
				$ret.='</tr>';
			}
			$ret .= '</table>';
		}
		return $ret;
	}
	
	public function getSalesreceipts()
	{

		$sql = "select a.salesreceipt_id, a.receipt_date, d.door_name, 
				if(a.tendertype<>'Card', a.amount, '') as 'Cash', if(a.tendertype='Card', a.amount, '') as 'Card' , 
				a.tendertype, b.fullname as 'cashier',a.occupancy_id 
				from salesreceipts a, users b, occupancy c, rooms d
				where a.update_by=b.user_id
				and a.occupancy_id=c.occupancy_id
				and c.room_id=d.room_id
				and a.receipt_date >='$this->start' and a.receipt_date <='$this->end'
				order by a.receipt_date
			";
		$res = mysql_query($sql) or die($sql);
		if(mysql_num_rows($res)) {
			$ret = '<h3>Sales Receipts Records</h3>';
			$ret .= '<table class=report>';
			$ret.='<tr><th>Date</th><th>Room No.</th><th>Cash</th><th>Card</th><th>TenderType</th><th>Cashier</th></tr>';
			while( list($sid,$date,$door,$cash,$card,$tender,$cashier,$occupancy)=mysql_fetch_row($res) ) {
				$ret.='<tr>';
				$ret.="<td>$date</td>";
				$ret.="<td class='numeric'>$door</td>";
				$ret.="<td class='numeric'>$cash</td>";
				$ret.="<td class='numeric'>$card</td>";
				$ret.="<td>$tender</td>";
				$ret.="<td>$cashier</td>";
				$ret.="<td class='debug'>$sid</td>";
				$ret.='<td class="debug"><a href="occupancydetails.php?occ='.$occupancy.'" target="_blank">'.$occupancy.'</a></td>';
				$ret.='</tr>';
				$this->total['cash']+=$cash;
				$this->total['card']+=$card;
			}
			$ret .= "<tr><th>&nbsp;</th><th>&nbsp;</th>
			<th class='numeric'>{$this->total['cash']}</th>
			<th class='numeric'>{$this->total['card']}</th>
			<th>&nbsp;</th><th>&nbsp;</th></tr>";
			$ret .= '</table>';
		}
		return $ret;
	}
	
	public function getPrintableSalesreceipts()
	{

		$sql = "select a.salesreceipt_id, a.receipt_date, d.door_name, 
				if(a.tendertype<>'Card', a.amount, '0') as 'Cash', if(a.tendertype='Card', a.amount, '0') as 'Card' , 
				a.tendertype, b.fullname as 'cashier',a.occupancy_id 
				from salesreceipts a, users b, occupancy c, rooms d
				where a.update_by=b.user_id
				and a.occupancy_id=c.occupancy_id
				and c.room_id=d.room_id
				and a.receipt_date >='$this->start' and a.receipt_date <='$this->end'
				order by a.receipt_date
			";
		$res = mysql_query($sql) or die($sql);
		if(mysql_num_rows($res)) {
			$ret = '=== SALES BREAKDOWN ===';
			$ret .= "\n\n";
			$ret.="Transaction Date\tRoom No.\tCash\tCard\n";
			while( list($sid,$date,$door,$cash,$card,$tender,$cashier,$occupancy)=mysql_fetch_row($res) ) {
				$ret.="\n";
				$ret.="$date \t";
				$ret.="$door \t\t";
				$ret.="$cash \t";
				$ret.="$card \t";
				//$ret.="$tender \t";
				
				$this->total['cash']+=$cash;
				$this->total['card']+=$card;
			}
			$ret .= "\n \t\t\tTotal:\t\t{$this->total['cash']} \t{$this->total['card']}\t\t";
			
		}
		return $ret;
	}
	
	public function getCumulativeTransactions()
	{

		$sql = "select a.cc_id, a.cc_date, a.transaction_amount,  a.current_amount, a.transaction_type,  b.fullname as 'cashier' 
				from current_cash a, users b
				where a.update_by=b.user_id
				and a.cc_date >='$this->start' and a.cc_date <='$this->end'
				order by a.cc_date
			";
		$res = mysql_query($sql) or die($sql);
		if(mysql_num_rows($res)) {
			$ret = '<h3>Transactions</h3>';
			$ret .= '<table class=report>';
			$ret.='<tr><th>Date</th><th>Transaction Amount</th><th>Running Amount</th><th>Transaction Type</th><th>Cashier</th></tr>';
			while( list($sid,$date,$tamt,$camt,$tender,$cashier)=mysql_fetch_row($res) ) {
				$ret.='<tr>';
				$ret.="<td>$date</td>";
				$ret.="<td class='numeric'>$tamt</td>";
				$ret.="<td class='numeric'>$camt</td>";
				$ret.="<td>$tender</td>";
				$ret.="<td>$cashier</td>";
				$ret.="<td>$sid</td>";
				$ret.='</tr>';
				if($tender=='In')
				$this->total['tamt']+=$tamt;

			}
			$ret .= "<tr><th>&nbsp;</th>
			<th class='numeric'>{$this->total['tamt']}</th>
			<th>&nbsp;</th>
			<th class='numeric'>&nbsp;</th>
			<th>&nbsp;</th><th>&nbsp;</th></tr>";
			$ret .= '</table>';
		}
		return $ret;
	}
}